package com.damai.utils;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.citywithincity.db.Db;
import com.citywithincity.db.Entity;
import com.citywithincity.db.EntityBuilder;
import com.citywithincity.db.EntityField;
import com.citywithincity.utils.DataUtil;
import com.citywithincity.utils.HttpUtil;
import com.citywithincity.utils.JsonUtil;
import com.citywithincity.utils.MapUtil;


public class SyncModel {

	/**
	 * 
	 * @param table
	 * @return
	 * @throws IOException 
	 */
	public int getCount(String table) throws IOException{
		
		Map<String,Object> where = new HashMap<String, Object>();
		where.put("1", 1);
		
		
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("select", " count(*) as COUNT ");
		
		map.put("where", JsonUtil.toJson(where));
		map.put("table", table);
		List<Map<String, Object>> list = getData(map);
		
		Map<String, Object> data = list.get(0);
		
		return DataUtil.getInt(data, "COUNT");
		
	}
	public List<Map<String, Object>> getList(int position,  int pageSize) throws IOException{
		data.put("where",JsonUtil.toJson( where ) );
		data.put("position", position);
		data.put("pageSize", pageSize);
		List<Map<String, Object>> list = getData(data);	
		
		return list;
	}
	
	
	public List<Map<String, Object>> getList() throws IOException{
		data.put("where",JsonUtil.toJson( where ) );
		data.put("position", 0);
		List<Map<String, Object>> list = getData(data);	
		
		return list;
	}
	
	public Map<String, Object> getRow() throws IOException{
		data.put("where",JsonUtil.toJson( where ) );
		data.put("position", 0);
		List<Map<String, Object>> list = getData(data);	
		if(list.size() > 0 ){
			return list.get(0);
		}
		return null;
	}
	
	
	@SuppressWarnings("unchecked")
	public <T> T getObject(Class<T> clazz){
		Entity<T> entity = EntityBuilder.getEntity(clazz);
		
		EntityField[] fields = entity.getFields();
		try {
			Object result = clazz.newInstance();
			Map<String, Object> data = getRow();
			if(data==null){
				return null;
			}
			for (EntityField entityField : fields) {
				Object value = data.get(entityField.getColumnName());
				try{
					entityField.set(result, DataUtil.convert(value,entityField.getField().getType()) );
				}catch(Exception e){
					e.printStackTrace();
				}
				
			}
			return (T) result;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
	}
	
	
	
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getData(Map<String, Object> map) throws IOException{
		String content = HttpUtil.rawPost("http://218.5.80.17:8092/index.php/api/query_api/index", 
				MapUtil.toQueryString(map));
		Map<String,Object> result = JsonUtil.read(content);
		List<Map<String, Object>> list = (List<Map<String, Object>>) result.get("result");
		return list;
	}
	
	private Map<String, Object> data = new HashMap<String, Object>();
	private Map<String, Object> where = new HashMap<String, Object>();
	
	
	public SyncModel table(String table){
		data.put("table", table);
		return this;
	}
	
	
	
	public SyncModel condition(String key,Object value){
		where.put(key, value);
		return this;
	}

	public SyncModel select(String select) {
	
		data.put("select", select);
		
		return this;
	}
	
	
	public void syncTable(String table,String id) throws IOException {
		table(table);
		int position = 0;
		int pageSize = 1000;
		while(true){
			List<Map<String, Object>> list = getList(position, pageSize);
			if(list.size()==0){
				break;
			}
			for (Map<String, Object> map : list) {
				map.remove("RNUM");
				Db.ar().set(map).insert(table,new String[]{id});
			}
			position += pageSize;
		}
		
		
		
	}
}
